package org.lmj.dim.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.lmj.dim.dto.UserDTO;
import org.lmj.dim.mapper.UserMapper;
import org.lmj.dim.po.ServerResponse;
import org.lmj.dim.po.UserEntity;
import org.lmj.dim.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * Created by lmj on 2021/3/12.
 */
@RestController
@RequestMapping
@Slf4j
public class LoginController {

    @Autowired
    UserMapper userMapper;

    @RequestMapping("/login")
    public ServerResponse login(@Valid @RequestBody UserDTO userDTO){
        QueryWrapper<UserEntity> objectQueryWrapper = new QueryWrapper<>();
        UserEntity dbUser = userMapper.selectOne(objectQueryWrapper.eq("username", userDTO.getUsername()));
        if (dbUser==null){
            return ServerResponse.fail(400,"该用户不存在");
        }
        if (SecurityUtils.matchesPassword(userDTO.getPassword(),dbUser.getPassword())){
            return ServerResponse.success("login success");
        }
      return ServerResponse.fail(400,"用户名或密码错误");
    }
}
